package com.ailot.cloud.upms.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ailot.cloud.upms.api.mapper.SysDictMapper;
import com.ailot.cloud.upms.api.mapper.SysDictValueMapper;
import com.ailot.cloud.upms.api.service.SysDictService;
import com.ailot.cloud.upms.common.entity.SysDict;
import com.ailot.cloud.upms.common.entity.SysDictValue;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author
 */
@Service
@AllArgsConstructor
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private final SysDictValueMapper sysDictValueMapper;

    /**
     * 根据ID 删除字典
     *
     * @param id 字典ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeDict(String id) {
        baseMapper.deleteById(id);
        sysDictValueMapper.delete(Wrappers.<SysDictValue>lambdaQuery()
                .eq(SysDictValue::getDictId, id));
    }

    /**
     * 更新字典
     *
     * @param dict 字典
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDict(SysDict dict) {
        this.updateById(dict);
        SysDictValue sysDictValue = new SysDictValue();
        sysDictValue.setType(dict.getType());
        sysDictValueMapper.update(sysDictValue, Wrappers.<SysDictValue>lambdaQuery()
                .eq(SysDictValue::getDictId, dict.getId()));
    }
}
